;(function(){
  'use strict';
  /**
   * customer 吴尧
   * @ngdoc function
   * @provinceName sasdApp.controller:volumeTrendCtrl
   * @description 物联网近30天业务成交量趋势统计
   * # customerAnalysisController
   * Controller of the sasApp
   */
  angular.module('controllers.pbossSysBusiness.pbossSysBusinessStastic')
    .controller('volumeTrendCtrl', volumeTrendCtrl);

  function volumeTrendCtrl($scope,$filter,$timeout,COREDATA,CoreService,CoreLoading,volumeTrendService,sysBusinessStatisticFactory) {
    $scope.errorInfo = COREDATA.ERRORINFO;   //日期错误信息
    $scope.provinces = COREDATA.PROVINCES;
    $scope.initQuery = function(){
      sysBusinessStatisticFactory.queryBusinessName(function(res){
        $scope.shoumun = 1;
        $scope.businessList = res.data;
        $scope.queryData();
      });
    };
    $scope.translate ={
      provinceCodes : [],
      searchTime: new Date(new Date().getFullYear(), new Date().getMonth(), new Date().getDate() - 1),
      businessCode:'BIP2B958'
    };

    var res = {};
    var xData;
    var yData;
    var businessName;
    var pbossEcharts = echarts.init(document.getElementById('pbossVolume'));
    $scope.queryData = function(){
      angular.forEach($scope.businessList, function(item){
        if($scope.translate.businessCode === item.businessCode){
          businessName = item.businessName;
        }
      });

      $scope.shoumun==1?$scope.shoumun=0:$scope.shoumun=1;
      $scope.send = {
        provinceCodes : [],
        searchTime : CoreService.dateFormat($scope.translate.searchTime, 'yyyyMMdd'),
        businessCode : $scope.translate.businessCode
      };
      angular.forEach($scope.translate.provinceCodes,function(pro){
        $scope.send.provinceCodes.push(pro.provinceCode);
      });
      if($scope.shoumun === 0){
        $scope.flagObj = true;
        CoreLoading.setState(true);
        xData = [];
        yData = [];
      volumeTrendService.queryVolumeTrend($scope.send,function(res){
        if(res.state === '10000' && res.data.length >= 1){
        angular.forEach(res.data, function(item){
          xData.push(item.time);
          yData.push(item.volumeTrends);
        });
      var itemStyle =  {
        normal: { // 系列级个性化，横向渐变填充
          borderRadius: 5
        }
      };
       var option = {
         title: {
           text: '近30天'+businessName+'业务成交量趋势统计',
           x: 'center'
         },
         tooltip: {
           trigger: 'axis',
           axisPointer: {
             type: 'shadow'
           }
         },
         legend: {
           orient: 'vertical',
           x: 'right',
           y: 'center',
           data: ['物联网']
         },
         grid: {
           x2: '8%'
         },
         xAxis: [
           {
             type: 'category',
             data: xData,
             axisLabel : {show:true,interval: 'auto',rotate: 45 }
           }
         ],
         yAxis: [
           {
             type: 'value' ,
             name: '(交易笔数)'
             // boundaryGap: [0, 0.01],
           }
         ],
         series: [
           {
             name: '物联网',
             type: 'bar',
             itemStyle: itemStyle,
             data: yData
           }
         ]
       };

       $timeout(function(){
        $scope.flagObj = true;
         CoreLoading.setState(false);
         pbossEcharts.clear();
         pbossEcharts.setOption(option);
       },1000);

       }else if(res.data.length === 0){
            CoreService.dialog('查询无数据');
        }else {
            CoreService.dialog('查询错误');
        }
        CoreLoading.setState(false);
        });
      }

    };
    $scope.queryData();
  }
})();

